<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

//拼接图片路径
function get_img_url($url, $basename, $size=0)
{
	switch ($size) {
		case '350':
			$pre = "350_";
			break;
		
		default:
			$pre = "";
			break;
	}

	$imgPath = rtrim($url,"/").'/';
	$imgPath .= substr($basename,0,4).'/';
	$imgPath .= substr($basename,4,2).'/';
	$imgPath .= substr($basename,6,2).'/';
	$imgPath .= $pre.$basename;
	return $imgPath;
}

/**
 * 发送邮件
 * @param $toemail 收件人email
 * @param $subject 邮件主题
 * @param $message 正文
 * @param $from 发件人
 * @param $cfg 邮件配置信息
 * @param $sitename 邮件站点名称
 */
function send_email($toemail, $subject, $message, $from = '', $cfg = array(), $sitename = '')
{
    //判断openssl是否开启
    $openssl_funcs = get_extension_funcs('openssl');
    if (!$openssl_funcs) {
        return array('status' => -1, 'msg' => '请先开启openssl扩展');
    }
    //表单提交 测试发送
    /*if ($cfg && is_array($cfg)) {
    $from = $cfg['from'];
    $email = $cfg;
    } else {
    $config = cache('Config');
    }*/
    // $config = cache('Config');

    $mail = new ven\PHPMailer\PHPMailer();
    //Server settings
    $mail->CharSet = 'UTF-8'; //设定邮件编码，默认ISO-8859-1，如果发中文此项必须设置，否则乱码
    $mail->SMTPDebug = 0; // Enable verbose debug output
    $mail->isSMTP(); // Set mailer to use SMTP
    $mail->Host = EMAIL_SMTP; // Specify main and backup SMTP servers
    $mail->SMTPAuth = true; // Enable SMTP authentication
    $mail->Username = EMAIL_NAME; // SMTP username
    $mail->Password = EMAIL_PWD; // SMTP password
    $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 25; //端口 - likely to be 25, 465 or 587

    //Recipients
    $mail->setFrom(EMAIL_NAME, EMAIL_NICKNAME); //发送方地址和昵称
    $mail->addAddress($toemail, 'Joe User'); // Add a recipient
    //$mail->addReplyTo('info@example.com', 'Information'); //回复地址

    //Content
    $mail->isHTML(true); // Set email format to HTML
    $mail->Subject = $subject; //标题
    $mail->Body = $message; //内容
    //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    // dump($mail->ErrorInfo);die;
    if (!$mail->send()) {
        return array('status' => -1, 'msg' => '发送失败: ' . $mail->ErrorInfo);
    } else {
        return array('status' => 1, 'msg' => '发送成功');
    }

}